package com.hzb.shop.autocode.file;

import java.io.*;
import java.util.*;

public class FileUtils {

	public static List<String> read(String path){
		List<String> list = new ArrayList<String>();
		FileReader reader = null;
		BufferedReader br = null;
        try{
        	reader = new FileReader(path);
	        br = new BufferedReader(reader);
	        String str = null;
	        while((str = br.readLine()) != null) {
	        	list.add(str);
	        }
	        
        }catch(Exception e){
        	e.printStackTrace();
        }finally{
        	try {
				br.close();
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        return list;
	}
	public static void write(List<String> list,String path){
//		 FileWriter writer = null;
//	     BufferedWriter bw = null;
	     OutputStream out = null;
		try{
			out = new FileOutputStream(path);
			for(String s:list){
				out.write((s+"\n").getBytes("utf-8"));
			}
//			writer = new FileWriter(path);
//			bw = new BufferedWriter(writer);
//	        final StringBuilder sb = new StringBuilder();
//	        list.stream().forEach(s->{
//	        	sb.append(s+"/n");
//	        });
//	        System.out.println(sb.toString());
//	        bw.write(sb.toString());
			System.out.println("write ok!");
        }catch(Exception e){
        	e.printStackTrace();
        }finally{
        	try {
        		out.close();
//        		bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
	}
	public static void main(String[] args) {
		FileWriter writer = null;
	     BufferedWriter bw = null;
		try{
			File f = new File("d:/test.xml");
			OutputStream out = new FileOutputStream(f);
//			out.write("测试java 文件操作\r\n".getBytes("UTF-8"));
			writer = new FileWriter("d:/test.xml");
			bw = new BufferedWriter(writer);
	        bw.write("fdagdf");
       }catch(Exception e){
       	e.printStackTrace();
       }
	}
}
